package com.lottery.ruok.lottrey.adapter;

import android.app.Activity;
import android.graphics.Color;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.activity.PrizeListActivity;
import com.lottery.ruok.lottrey.common.Common;
import com.lottery.ruok.lottrey.retrofitModule.response.LotteryStatOneResponse;
import com.lottery.ruok.lottrey.utils.TimeUtil;
import com.nex3z.flowlayout.FlowLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import ui.AreaCircle;
import utils.Utils;

/**
 * Created by ASUS on 2017/11/13.
 */

public class PrizeNumberAdapter extends BaseAdapter {
    private String key;
    private Activity fragmentActivity;
    private LotteryStatOneResponse lotteryStatOneResponses;

    public PrizeNumberAdapter(String key, Activity fragmentActivity, LotteryStatOneResponse lotteryStatOneResponses) {
        this.key = key;
        this.fragmentActivity = fragmentActivity;
        this.lotteryStatOneResponses = lotteryStatOneResponses;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view != null) {
            holder = (ViewHolder) view.getTag();
        } else {
            view = LayoutInflater.from(fragmentActivity).inflate(R.layout.prize_number_item, viewGroup, false);
            holder = new ViewHolder(view);
            view.setTag(holder);
        }

        switch (key) {
            case "号码": {
                String code = lotteryStatOneResponses.getItem_code();
                String[] temp = lotteryStatOneResponses.getIssue_list().get(i).getPrize_num().split(",");
                holder.container.removeAllViews();
                if (temp != null) {
                    for (String s : temp) {
                        AreaCircle areaCircle = new AreaCircle(fragmentActivity);
                        Common.setBallBackground(areaCircle, s, code);
                        areaCircle.setText(s);
                        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(Utils.dip2px(fragmentActivity, 20), Utils.dip2px(fragmentActivity, 20));
                        areaCircle.setLayoutParams(layoutParams);
                        holder.container.addView(areaCircle);
                    }
                }
                break;
            }
            case "大小":
            case "单双": {
                String[] temp;
                if (key.equals("单双")) {
                    temp = lotteryStatOneResponses.getIssue_list().get(i).getDanshuang().split(",");
                } else {
                    temp = lotteryStatOneResponses.getIssue_list().get(i).getDaxiao().split(",");
                }
                holder.container.removeAllViews();
                if (temp != null) {
                    for (String s : temp) {
                        AreaCircle areaCircle = new AreaCircle(fragmentActivity);
                        switch (s) {
                            case "小":
                                areaCircle.setBackground(Color.parseColor("#9076d2"));
                                break;
                            case "大":
                                areaCircle.setBackground(Color.parseColor("#f6a06f"));
                                break;
                            case "单":
                                areaCircle.setBackground(Color.parseColor("#dbbf91"));
                                break;
                            case "双":
                                areaCircle.setBackground(Color.parseColor("#90bfe1"));
                                break;
                            case "和":
                                areaCircle.setBackground(Color.parseColor("#81b272"));
                                break;
                        }
                        areaCircle.setText(s);
                        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(Utils.dip2px(fragmentActivity, 20), Utils.dip2px(fragmentActivity, 20));
                        areaCircle.setLayoutParams(layoutParams);
                        holder.container.addView(areaCircle);
                    }
                }
                break;
            }
            default: {
                String[] temp = lotteryStatOneResponses.getIssue_list().get(i).getZonghe().split(",");
                int k = 0;
                holder.container.removeAllViews();
                if (temp != null) {
                    for (String s : temp) {
                        TextView textView = new TextView(fragmentActivity);
                        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                        if (k == 0) {
                            textView.setTextColor(Color.RED);
                        } else {
                            textView.setTextColor(fragmentActivity.getResources().getColor(R.color.font_black));
                        }
                        textView.setLayoutParams(layoutParams);
                        textView.setText(s);
                        holder.container.addView(textView);

                        k++;
                    }
                }
                break;
            }
        }
        if ("fc3d".equals(lotteryStatOneResponses.getItem_code()) || "pl3".equals(lotteryStatOneResponses.getItem_code())) {
            String issue_date = lotteryStatOneResponses.getIssue_list().get(i).getIssue_date();
            holder.timeNo.setText(TimeUtil.getDayFormatTime(TimeUtil.MONTH_DAY_DATE_FORMAT,issue_date));
        } else {
            holder.timeNo.setText((lotteryStatOneResponses.getIssue_list().get(i).getOpen_time().endsWith(":00  ") ? lotteryStatOneResponses.getIssue_list().get(i).getOpen_time().substring(0, lotteryStatOneResponses.getIssue_list().get(i).getOpen_time().length() - 5) + "  " : lotteryStatOneResponses.getIssue_list().get(i).getOpen_time()) + lotteryStatOneResponses.getIssue_list().get(i).getIssue_no());
        }

        return view;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public int getCount() {
        if (lotteryStatOneResponses != null) {
            return lotteryStatOneResponses.getIssue_list().size();
        }
        return 0;
    }

    static class ViewHolder {
        @BindView(R.id.time_no)
        TextView timeNo;
        @BindView(R.id.container)
        FlowLayout container;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
